package com.dailyblue.java.spring.boot.redis.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/list")
@Slf4j
public class ListRedisController {
    @Resource(name = "stringRedisTemplate")
    private StringRedisTemplate redisTemplate;

    @GetMapping
    public void execute() {
        ListOperations operations = redisTemplate.opsForList();
        operations.leftPush("A", "aa");
        operations.leftPush("A", "bb");
        operations.leftPush("A", "cc");
        operations.leftPush("A", "dd");
        List<String> list1 = operations.range("A", 0, -1);
        log.info("list1:{}", list1);
        operations.leftPushAll("A", "cc", "ee", "gg", "ii");
        List<String> list2 = operations.range("A", 0, -1);
        log.info("list2:{}", list2);
        operations.rightPush("A", "99");
        operations.rightPush("A", "88");
        operations.rightPush("A", "77");
        operations.rightPushAll("A", "66", "55");
        List<String> list3 = operations.range("A", 0, -1);
        log.info("list3:{}", list3);
        long len = operations.size("A");
        log.info("len:{}", len);
        operations.set("A", 3, "mm");
        operations.set("A", len - 1, "mm");
        Object first = operations.leftPop("A");
        Object last = operations.rightPop("A");
        List<String> list4 = operations.range("A", 0, -1);
        log.info("目前集合：{},first:{},last:{}", list4, first, last);
        Object v = operations.index("A", 4);
        log.info("5号位置是:{}", v);
        operations.remove("A", 3, "aa");
    }
}
